﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMUpdateBOM : BaseQryRpt 
    {
        public frmRMUpdateBOM()
        {
            InitializeComponent();
        }

        private void btnCondition_Click(object sender, EventArgs e)
        {
            if (chkIncludeRecBOM.Checked == false && chkIncludeCost.Checked == false 
                 && chkIncludeRecSalary.Checked==false && chkIncludeRecalSemi.Checked==false ) return;
            
            if (MessageBox.Show("您确定要开始此次操作？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                SysVar.UserCompany = "RECAL";
                this.Cursor = Cursors.WaitCursor;
                this.progressBar1.Visible = true;
                progressBar1.Visible = true;
                if (chkIncludeCost.Checked)
                {
                    RecalCost(); 
                }
                if (chkIncludeRecBOM.Checked)
                {
                    RecalBOM(); 
                }
                if (chkIncludeRecSalary.Checked)
                {
                    RecalSalary();
                }
                if (chkIncludeRecalSemi.Checked)
                {
                    RecalSemi(); 
                }
                this.progressBar1.Visible = false;
                DataFunctions.AddLog( new DBAccess(false,false),  "完成重算 批量更新。", "RM", "");
                MsgBoxEx.Show("更新完成！", "PDM");
                SysVar.UserCompany = "";
            }
        }

        private void RecalCost()
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = "select * from imCategoryWKWorkCost order by CategoryID,WKProductID,WKPriceType";
            DataTable dtItem = dbcon.GetDataTable(strSQL);
            foreach (DataRow drItem in dtItem.Rows)
            {
                try
                {
                    dbcon.BeginTran();
                    if (!DataFunctions.UpdateBOMCOSTByWKWorkCost(dbcon, drItem, this.progressBar1))
                    {
                        dbcon.RollbackTran();
                        continue;
                    }
                    else
                    {
                        dbcon.CommitTran();
                    }
                }
                catch
                {
                    dbcon.RollbackTran();
                    continue;
                }
            }
        }

        private void RecalSalary()
        {
            DBAccess dbcon = new DBAccess(false, false);
            try
            {
                dbcon.BeginTran();
                if (chkIncludeRecSalary.Checked)
                {
                    if (!DataFunctions.UpdateSalary(dbcon, this.progressBar1))
                    {
                        dbcon.RollbackTran();
                    }
                    else
                    {
                        dbcon.CommitTran();
                    }
                }
            }
            catch
            {
                dbcon.RollbackTran();
            }
            progressBar1.Refresh();
            this.Cursor = Cursors.Default;

            dbcon.CloseCon(); 

        }

        private void RecalSemi()
        {
            DBAccess dbcon = new DBAccess(false, false);
            try
            {
                dbcon.BeginTran();
                if (chkIncludeRecBOM.Checked)
                {
                    if (!DataFunctions.UpdateBOMSemi(dbcon, this.progressBar1))
                    {
                        dbcon.RollbackTran();
                    }
                    else
                    {
                        dbcon.CommitTran();
                    }
                }
            }
            catch
            {
                dbcon.RollbackTran();
            }
            progressBar1.Refresh();
            this.Cursor = Cursors.Default;

            dbcon.CloseCon(); 
        }

        private void RecalBOM()
        {
            DBAccess dbcon = new DBAccess(false, false);
            try
            {
                dbcon.BeginTran();
                if (chkIncludeRecBOM.Checked)
                {
                    if (!DataFunctions.UpdateBOMByItem(dbcon, this.progressBar1))
                    {
                        dbcon.RollbackTran();                     
                    }
                    else
                    {
                        dbcon.CommitTran();
                    }
                }
            }
            catch
            {
                dbcon.RollbackTran();
            }
            progressBar1.Refresh();
            this.Cursor = Cursors.Default;
            
            dbcon.CloseCon(); 
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }


    }
}
